<?php

	class Page{
		
		//public $orderflag='id';
		public $pageNo=1;		//页码
		public $pageSize=20;	//页尺寸
		public $count=0;		//数据总数
		public $pageCount=0;	//总页数
		public $pageNext=0;		//下一页
		public $pagePrev=0;		//上一页
		private $tableName;		//表名
		public $wherepid='';
		public $sort='id';
		public $order='asc';
		public function __construct($tname){
			$this->tableName=$tname;
		}
		
		public function init(){
			$db=MysqlDB::getInstance();
			$query="select count(*) as num from $this->tableName";
			$db->query($query);
			$row=$db->fetch_assoc();
			$this->count=$row['num'];	//数据总数
			$this->pageCount=ceil($this->count/$this->pageSize);	//总页数
			$this->pageNo=isset($_GET['pageNo'])?$_GET['pageNo']:1;	//页码
			$this->pageNext=$this->pageNo+1;	//下一页
			$this->pagePrev=$this->pageNo-1;	//上一页
			//判断页码越界
			if($this->pageNext>$this->pageCount) $this->pageNext=$this->pageCount;
			if($this->pagePrev<1) $this->pagePrev=1;	
			if($this->pageNo>$this->pageCount) $this->pageNo=$this->pageCount;
			if($this->pageNo<1) $this->pageNo=1;	
			
			$offset=($this->pageNo-1)*$this->pageSize;	//偏移量
			if($this->wherepid==''){
				$query="select * from $this->tableName order by $this->sort $this->order limit $offset,$this->pageSize";
			}else{
				//$query="select * from $this->tableName where cid=$this->wherepid order by $this->orderflag limit $offset,$this->pageSize";
				$query="select gd.* from goods as gd,goodsCate as gc where gd.cid=gc.cateID and gc.catePID=$this->wherepid order by gd."."$this->sort $this->order limit $offset,$this->pageSize";
			}
			//dump($query);
			$db->query($query);
			$num=$db->num_rows();	//本页查询到的数据总数
			$data=array();			//定义空数组
			for($i=0;$i<$num;$i++){	//循环num次
				$data[]=$db->fetch_assoc();	//创建二维数组
			}
			return $data;
			
		}
		
		//获取分页信息
		public function getPager(){
			$str='共'.$this->pageCount.'页 ';
			$str.='共'.$this->count.'条数据 ';
			$str.='当前第'.$this->pageNo.'页 ';
			$str.='每页'.$this->pageSize.'条 ';
			$str.="<a href=/index.php/Home/Index/index/pageNo/1>第一页</a>";
			$str.='&nbsp;&nbsp;';
			$str.="<a href=/index.php/Home/Index/index/pageNo/{$this->pagePrev}>上一页</a>";
			$str.='&nbsp;&nbsp;';
			$str.="<a href=/index.php/Home/Index/index/pageNo/{$this->pageNext}>下一页</a>";
			$str.='&nbsp;&nbsp;';
			$str.="<a href=/index.php/Home/Index/index/pageNo/{$this->pageCount}>最末页</a>";
			return $str;
		}
		
		
		
		
	}